(function() {
  $(function() {
    var form, record;
    form = $('form.main');
    form.form({
      inline: true,
      fields: {
        name: 'empty',
        chnl: 'empty',
        merid: 'empty'
      }
    });
    record = form.form('get value', 'record');
    record = record.length > 0 ? JSON.parse(record) : null;
    form.find('.dropdown.chnl').dropdown({
      onChange: function(value, text, $choice) {
        $(this).addClass('loading');
        return $.post('/p1/rule/merids', {
          chnl: value
        }, function(resp) {
          var items, merid, _i, _len, _ref;
          form.find('.dropdown.chnl').removeClass('loading');
          if (!resp.succ) {
            return;
          }
          form.find('.dropdown.merid').dropdown('clear');
          items = '';
          _ref = resp.merids;
          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            merid = _ref[_i];
            items += "<div class='item' data-value='" + merid.uuid + "'>";
            items += "" + (merid.name.substring(0, 25));
            items += "<div class='right floated' style='font-size:small; color:#888;'>" + merid.merno + "</div>";
            items += "</div>";
          }
          form.find('.dropdown.merid .menu').html(items);
          form.find('.dropdown.merid').dropdown('refresh');
          if (record && record.merid) {
            return form.form('set value', 'merid', record.merid);
          }
        });
      }
    });
    if (record && record.chnl) {
      return form.form('set value', 'chnl', record.chnl);
    }
  });

}).call(this);
